import { followOrUnfollow } from '@/api/consult'
import type { FollowType } from '@/types/consult'
import { ref } from 'vue'

export default function useToggleFollow(type: FollowType = 'doc') {
  // 切换关注逻辑
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      // id: 被关注目标的id： likeFlag： 被关注目标和当前登录人的关注关系；
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
      loading.value = false
    } catch {
      loading.value = false
    }
  }

  return { loading, follow }
}
